package xupt.com.bolg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xupt.com.bolg.entity.User;
import xupt.com.bolg.service.impl.BolgServiceImpl;
import xupt.com.bolg.service.impl.UserServiceImpl;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ManagerUserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    BolgServiceImpl bolgService;
    @GetMapping("/home")
    public String skip(){
        return "homepage";
    }


    @GetMapping("/manager")
    public String skipManager(HttpServletRequest request){
        String  name =(String) request.getSession().getAttribute("name");
        User byName = userService.findByName(name);
        if(byName.getManager()==1){

            return "manager/manager";
        }else {
            request.setAttribute("msg","您不是管理员");
            return "homepage";
        }
    }

    @GetMapping("/manager/userlist")
    public String listUser(Model model){
        List<User> all = userService.findAll();
        model.addAttribute("users",all);
        return "user/list";
    }
    //删除用户时必须删除他的所有bolg and cloumn
    @GetMapping("/manager/user/delete")
    public String lookUser(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        Integer integer = Integer.valueOf(id);
        User byID = userService.findByID(integer);
        if(byID.getManager()==1){
            request.setAttribute("msg","您不能删除管理员");
            List<User> all = userService.findAll();
            model.addAttribute("users",all);
            return "user/list";
        }
        //删除用户
        userService.deleteByID(integer);
        //删除他所有的博客
        bolgService.deleteByUserName(byID.getName());
        return "redirect:/manager/userlist";
    }

    @GetMapping("/manager/user/skip")
    public String updateUser(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        Integer integer = Integer.valueOf(id);
        User byID = userService.findByID(integer);
        if(byID.getManager()==1){
            request.setAttribute("msg","您不能修改管理员信息");
            List<User> all = userService.findAll();
            model.addAttribute("users",all);
            return "user/list";
        }
        request.getSession().setAttribute("updateUserID",id);
        model.addAttribute("user",byID);
        return "user/manainfor";
    }

    @PostMapping("/manager/user/update")
    public String update(User user, Model model,HttpServletRequest request){
        String  id =(String) request.getSession().getAttribute("updateUserID");
        Integer integer = Integer.valueOf(id);
        request.getSession().removeAttribute("updateUserID");
        user.setId(integer);
        if(userService.findByID(integer).getManager()==1){
            request.setAttribute("msg","您不能对管理员进行操作");
            model.addAttribute("users",userService.findAll());
            return "user/list";
        }
        userService.update(user);
        model.addAttribute("users",userService.findAll());
        return "user/list";
    }
    @PostMapping("/manager/findUserByName")
    public String findUserByName(@RequestParam(name ="name")String name,Model model){
        User byName = userService.findByName(name);
        model.addAttribute("users",byName);
        return "user/list";
    }
}
